<?php

namespace app\common\model\hfztcy;

use app\common\model\BaseModel;
use app\common\model\LibAdmin;
use app\common\model\LibUser;
use common\Common;
use think\Db;
use think\Exception;

// 单次付费看视频
//direct_user_id 直推用户
//private_red_mother_id 专属红娘用户
//shi_admin_id 市代用户
//xian_admin_id 区县代用户

// 开通红娘
//direct_user_id 直推红娘
//indirect_user_id 间推红娘
//shi_admin_id 市代用户
//xian_admin_id 区县代用户
//kf_admin_id 客服部用户
//px_admin_id 培训部用户

// vip套餐订单
//shi_admin_id 市代用户
//xian_admin_id 区县代用户

// 栏目认证
//direct_user_id 直推红娘
//indirect_user_id 间推红娘
//store_user_id 门店用户
//store_parent_user_id 门店上级用户
//shi_admin_id 市代用户
//xian_admin_id 区县代用户
//kf_admin_id 客服部用户
//px_admin_id 培训部用户

/**
 * 分销逻辑
 * @name LibDistribute.php
 * @package hfztcy
 * @category model
 * @link http://www.chanekeji.com
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2022-06-17
 */
class LibDistribute extends BaseModel
{
    public $order;
    private $logPath;

    private $memberModel;
    private $userModel;
    private $adminModel;
    private $curUserInfo;
    private $distribute;
    private $balanceRecords;
    private $adminBalanceRecords;

    public $orderType = 0;

    public function __construct($order)
    {
        $this->order = $order;
        $this->logPath = ROOT_PATH . "public/pay/pay{$order['id']}.log";
        parent::__construct();
    }

    /**
     * Info: 设置订单类型 0单次看视频订单1VIP套餐订单2红娘开通订单3栏目认证订单
     * Date: 2023/1/9 15:32
     */
    public function setOrderType ($type)
    {
        $this->orderType = $type;
        return $this;
    }

    private function write_file($text)
    {
        if (@$fp = fopen($this->logPath, 'a')) {
            flock($fp, 2);
            fwrite($fp, $text . "\r\n");
            fclose($fp);
        }
    }


    /**
     * Info: 直推用户【单次付费看视频】【红娘认证】【栏目认证】
     * Date: 2022/11/18 9:19
     */
    private function getDirectUser()
    {
        $obj = $this->distribute->DirectUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByDirectUserId ();
    }

    /**
     * Info: 间推用户【红娘认证】【栏目认证】
     * Date: 2022/11/18 9:43
     */
    private function getIndirectUser()
    {
        $obj = $this->distribute->IndirectUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByIndirectUserId();
    }

    /**
     * Info: 专属红娘【单次付费看视频】
     * Date: 2022/11/18 9:44
     */
    private function getPrivateRedMotherUser()
    {
        $obj = $this->distribute->PrivateRedMotherUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByPrivateRedMotherUserId();
    }

    /**
     * Info: 门店【单次付费看视频】
     * Date: 2022/11/18 9:44
     */
    private function getStoreBySeeVideo ()
    {
        $obj = $this->distribute->StoreUserBySeeVideoCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByStoreUserId();
    }

    /**
     * Info: 市代理商【单次付费看视频】【红娘认证】【栏目认证】【VIP套餐】
     * Date: 2022/11/18 9:44
     */
    private function getAgentByShi()
    {
        $obj = $this->distribute->ShiAgentCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByShiAgentUserId();
    }

    /**
     * Info: 区县代理商【单次付费看视频】【红娘认证】【栏目认证】【VIP套餐】
     * Date: 2022/11/18 9:45
     */
    private function getAgentByXian()
    {
        $obj = $this->distribute->XianAgentCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByXianAdminId();
    }

    /**
     * Info: 客服部【红娘认证】【栏目认证】
     * Date: 2022/11/18 9:46
     */
    private function getCustomerUser()
    {
        $obj = $this->distribute->CustomerUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByKfAdminId();
    }

    /**
     * Info: 培训部 【红娘认证】【栏目认证】
     * Date: 2022/11/18 9:46
     */
    private function getTrainUser()
    {
        $obj = $this->distribute->TrainUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByPxAdminId();
    }


    // 1、先处理门店分润
    // 2、在处理门店上级推荐人分润比例
    /**
     * Info: 门店 【栏目认证】
     * Date: 2022/11/18 9:47
     */
    private function getStore()
    {
        $obj = $this->distribute->StoreUserCommission;
        $obj->setOrderType($this->orderType);
        $obj->setOrderInfo($this->order);
        $obj->setBaseUserModel($this->userModel);
        $obj->setMemberModel($this->memberModel);
        $obj->setAdminModel($this->adminModel);
        $obj->setCurPayUserInfo($this->curUserInfo);
        $obj->setBalanceRecordsModel($this->balanceRecords);
        $obj->setAdminBalanceRecordsModel($this->adminBalanceRecords);
        $obj->setLogPath($this->logPath);
        return $obj->getCommissionByStoreUserId();
    }

    /**
     * Info: 分销方法
     * Date: 2022/11/18 9:47
     */
    public function updateUserBalance()
    {
        $this->memberModel = new LibMember();
        $this->adminModel = new LibAdmin();
        $this->userModel = new LibUser();
        $this->distribute = new \app\common\model\distribute\base();
        $this->balanceRecords = new LibBalanceRecords();
        $this->adminBalanceRecords = new LibAdminBalanceRecords();
        // 佣金类型
        $arr = [0 => '单次看视频订单', 1=>'VIP套餐订单',2 => '红娘开通订单', 3 => '栏目认证订单'];
        $this->write_file('佣金类型：' . $arr[$this->orderType]);

        $result = [];
        Db::startTrans();
        try {
            $configList = [
                'see_video_rate',
                'vip_model_rate',
                'red_mother_auth_rate',
                'vip_video_auth_rate',
            ];
            $this->curUserInfo = $this->memberModel->findMember(['user_id' => $this->order['user_id']]);
            $this->write_file('当前用户：' . $this->order['user_id']);
            $configModel = new LibConfig();
            $findConfigRes = $configModel->findConfig(['item_key' => $configList[$this->orderType]]);
            $this->write_file('分销配置：' . $findConfigRes['data']['item_value']);
            // 直推用户【单次付费看视频】【红娘认证】【栏目认证】
            $result[0] = $this->getDirectUser();
            // 间推用户【红娘认证】【栏目认证】
            $result[1] = $this->getIndirectUser();
            // 专属红娘【单次付费看视频】
            $result[2] = $this->getPrivateRedMotherUser();
            // 市代理商【单次付费看视频】【红娘认证】【栏目认证】【VIP套餐】
            $result[3] = $this->getAgentByShi();
            // 区县代理商【单次付费看视频】【红娘认证】【栏目认证】【VIP套餐】
            $result[4] = $this->getAgentByXian();
            // 客服部【红娘认证】【栏目认证】
            $result[5] = $this->getCustomerUser();
            // 培训部 【红娘认证】【栏目认证】
            $result[6] = $this->getTrainUser();
            // 门店
            if ($this->order['type'] == 2) {
                // 门店 【栏目认证】
                $result[7] = $this->getStore();
            } else {
                // 门店【单次付费看视频】
                $result[7] = $this->getStoreBySeeVideo();
            }

            // 更新佣金发放状态
            (new LibOrder())->updateOrder(['id' => $this->order['id']] , ['delivery_state' => 1,'delivery_time' => Common::getTime()]);
            Db::commit();
        } catch (Exception $exception) {
            Db::rollback();
        }
        return $result;
    }
}
?>